// pages/home/home.js
var app = getApp()
var changeName = 'hot'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        key: [],
        region: ['山西省', '晋中市', '榆次区'],
        containShow: true,
        searchShow: false,
        allNeedChange: []

    },
    setValue: function (e) {
        console.log(e)
        console.log(e.currentTarget.dataset.postid, e.currentTarget.dataset.id)
        var that = this;
        var allProduct = this.data.key
        if (allProduct[e.currentTarget.dataset.id].selected == 0)
            allProduct[e.currentTarget.dataset.id].selected = 1
        else {
            allProduct[e.currentTarget.dataset.id].selected = 0
        }
        that.setData({
            key: allProduct
        })
    },
    getLoad: function () {
        var item = []
        for (var i = 0; i < this.data.key.length; i++) {
            if (this.data.key[i].selected == 1) {
                item.push(this.data.key[i].id)
                let tableName = 'product'
                let recordID = this.data.key[i].id // 数据行 id

                let Product = new wx.BaaS.TableObject(tableName)
                let product = Product.getWithoutData(recordID)

                product.set(changeName, 1)
                product.update().then(res => {

                    console.log(res)
                    // success
                }, err => {
                    // err
                })
            }
        }
        console.log(changeName)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    // 删注释就可以了

        if(options.name==1)changeName = 'hot'
        else if(options.name==2)changeName= 'popular'
        else if (options.name==3) changeName= 'snacks'
        else changeName='beauty'




        app.globalData.BarHeight = wx.getSystemInfoSync().statusBarHeight + 44
        this.setData({
            BarHeight: app.globalData.BarHeight
        })
        this.onProduct();
        this.onChecked();
        this.onService();
        this.onService_items();

        console.log(this.data.key)
        var that = this
        wx.getLocation({
            type: 'gcj02',
            success: function (res) {
                //赋值经纬
                app.globalData.latitude = res.latitude
                app.globalData.longitude = res.longitude
                console.log(app.globalData.latitude)
                console.log(app.globalData.longitude)
            }
        })

    },
    onService_items() {
        let tableName = 'service_items'

        let Query = new wx.BaaS.Query()
        // Query.compare('product_code', '=', that.data.cart[i].product_code)
        let Product = new wx.BaaS.TableObject(tableName)
        Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
            // success
            console.log(res)
            app.globalData.service_items = res.data.objects //知晓云获取产品内容
            app.globalData.service_items_sum = res.data.objects.length
            console.log('service_items:' + app.globalData.service)
            console.log('service_items_sum:' + app.globalData.service_sum)
        }, err => {
            // err
        })
    },
    onService() {
        let tableName = 'service'

        let Query = new wx.BaaS.Query()
        // Query.compare('product_code', '=', that.data.cart[i].product_code)
        let Product = new wx.BaaS.TableObject(tableName)
        Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
            // success
            console.log(res)
            app.globalData.service = res.data.objects //知晓云获取产品内容
            app.globalData.service_sum = res.data.objects.length
            console.log('service:' + app.globalData.service)
            console.log('service_sum:' + app.globalData.service_sum)
        }, err => {
            // err
        })
    },
    //获取所有产品信息
    onProduct() {
        let tableName = 'product'

        let Query = new wx.BaaS.Query()
        // Query.compare('product_code', '=', that.data.cart[i].product_code)
        let Product = new wx.BaaS.TableObject(tableName)
        Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
            // success
            console.log(res)
            app.globalData.product = res.data.objects //知晓云获取产品内容
            app.globalData.product_sum = res.data.objects.length
            console.log('product:' + app.globalData.product)
            console.log('product_sum:' + app.globalData.product_sum)
        }, err => {
            // err
        })
    },
    onChecked() {
        //热门商品
        var that = this
        let tableName = 'product'

        let Query = new wx.BaaS.Query()
        Query.compare('hot', '!=', 2)
        let Product = new wx.BaaS.TableObject(tableName)
        Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
            // success
            console.log(res)
            that.setData({
                key: res.data.objects, //热门商品
            })
            console.log(that.data.key)
        }, err => {
            // err
        })
    },

    onBindFocus() {
        wx.navigateTo({
            url: '../search/search'

        })
    },

    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    },

    shouCang() {
        wx.navigateTo({
            url: '../shoucang/shoucang',
        })
    },

    shangpinDetail(e) {
        console.log(e.currentTarget.dataset)
        var postId = e.currentTarget.dataset.postid
        setTimeout(function (e) {
            wx.navigateTo({
                url: '../post-detail/post-detail?id=' + postId
            })
        }, 200)

    },

    gengDuo(e) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        setTimeout(function () {
            wx.stopPullDownRefresh();
            wx.showToast({
                title: '刷新成功',
                icon: 'none'
            })
        }, 1000);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})